//
//  XBGetVerificationCodeView.m
//  demoGetVerificationCode
//
//  Created by MacBook on 2018/10/3.
//  Copyright © 2018年 MacBook. All rights reserved.
//

#import "XBGetVerificationCodeView.h"

@interface XBGetVerificationCodeView()

@property(nonatomic,strong)UIButton * btn;

@end

@implementation XBGetVerificationCodeView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self)
    {
        self.verificationNumber = 6;
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.btn setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        
        [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        [self.btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        
        [self.btn addTarget:self action:@selector(setBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.btn];
    }
    
    return self;
}

-(void)setBtnAction:(UIButton *)sender
{
    if(sender && [sender isKindOfClass:[UIButton class]])
    {
        [self.btn setTitle:[NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]] forState:UIControlStateNormal];
        
        if(self.setShowCodeNumber)
        {
            self.setShowCodeNumber([NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]]);
        }
        
    }
}

-(void)setCodeTitleColor:(UIColor *)color
{
    [self.btn setTitleColor:color forState:UIControlStateNormal];
}
-(void)setCodeTitleFont:(UIFont *)font
{
    [self.btn.titleLabel setFont:font];
}
-(NSString *)getRandomStringWithNum:(NSInteger)num
{
    
    NSString *string = [[NSString alloc]init];
    
    if(self.tmpCodeType == XBGetVerificationCodeOne)
    {
        for (int i = 0; i < num; i++) {
           
            int figure = arc4random() % 10;
           
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
           
            string = [string stringByAppendingString:tempString];
           
        }
        return string;
    }
    else if(self.tmpCodeType == XBGetVerificationCodeTwo)
    {
        for (int i = 0; i < num; i++) {
            
            int figure = (arc4random() % 26) + 97;
            
            char character = figure;
           
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
           
            string = [string stringByAppendingString:tempString];
            
        }
        return string;
    }
    else
    {
        for (int i = 0; i < num; i++) {
            int number = arc4random() % 36;
            if (number < 10) {
                int figure = arc4random() % 10;
                NSString *tempString = [NSString stringWithFormat:@"%d", figure];
                string = [string stringByAppendingString:tempString];
            }else {
                int figure = (arc4random() % 26) + 97;
                char character = figure;
                NSString *tempString = [NSString stringWithFormat:@"%c", character];
                string = [string stringByAppendingString:tempString];
            }
        }
        return string;
    }
    
    
}
@end
